<?php

namespace App\Console\Commands;

use App\Models\Comments;
use Illuminate\Console\Command;

class Messages extends Command
{
    public $ws;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'messages {action?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'messages';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $action = $this->argument('action');
        switch ($action) {
            case 'restart':
                $this->info('message server restarted');
                break;
            case 'close':
                $this->info('message server stoped');
                break;
            default:
                $this->start();
                break;
        }
    }

    public function start()
    {
        //创建websocket服务器对象，监听0.0.0.0:9501端口
        $this->ws = new \swoole_websocket_server("0.0.0.0", 9502);

//        $this->ws->set([
//            'heartbeat_check_interval' => 60, 'heartbeat_idle_time' => 600,
//        ]);

        //开启ssl模式
//        $this->ws = new \swoole_websocket_server("0.0.0.0", 9501,SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);

        //配置ssl模式
//        $this->ws->set([
//            'ssl_key_file' => '/var/www/html/ssl/4033464_cxh520.top.key',
//            'ssl_cert_file' => '/var/www/html/ssl/4033464_cxh520.top.pem'
//        ]);

        //监听WebSocket连接打开事件
        $this->ws->on('open',[$this,'open']);

        //监听WebSocket消息事件
        $this->ws->on('message',[$this,'message']);

        //监听WebSocket主动推送消息事件
        $this->ws->on('request',[$this,'request']);

        //监听WebSocket连接关闭事件
        $this->ws->on('close',[$this,'close']);

        $this->ws->start();
    }

    /**
     * 建立连接
     * @param $ws
     * @param $request
     */
    public function open($ws, $request){
        $count = Comments::where('status',0)->count();
        $this->ws->push($request->fd , $count);
    }

    /**
     * 接收消息
     * @param $ws
     * @param $frame
     */
    public function message($ws,$frame){
        if($frame->data == 0){
            Comments::where('status',0)->update(['status'=>1]);
        }
        foreach ($this->ws->connections as $fd) {
           $this->ws->push($fd,$frame->data);
         }

    }

    /**
     * 接收请求
     * @param $request
     * @param $response
     */
    public function request($request,$response){

        foreach ($this->ws->connections as $fd) {
            // 需要先判断是否是正确的websocket连接，否则有可能会push失败
            if ($this->ws->isEstablished($fd)) {
                $this->ws->push($fd, $request->post['message']);
            }
        }
    }

    /**
     * 关闭连接
     * @param $ws
     * @param $fd
     */
    public function close($ws,$fd){

    }
}
